#include <iostream>
#include <vector>
#include <queue>
using namespace std;
char maze[10][10];
int dir[4][2] = {1, 0, -1, 0, 0, 1, 0, -1};
bool flag;
/*
    递归解决
*/
void bfs(int xs, int ys, int m)
{
    int x, y;
    for (size_t i = 0; i < 4; i++)
    {
        x = xs;
        y = ys;
        x = x + dir[i][0]; // 上下左右移动
        y = y + dir[i][1];
        if (x >= 0 && x <= m - 1 && y >= 0 && y <= m - 1 && maze[x][y] == '.') // 判断移动后的点是否合法
        {
            maze[x][y] = 'S';
            bfs(x, y, m); // 进行递归
        }
        else if (maze[x][y] == 'E')
        {
            flag = true;
            return;
        }
    }
    return;
}
int main()
{
    int K, m, T, xs, ys;
    cin >> K;
    while (K--)
    {
        flag = false;
        cin >> m >> T;
        for (size_t i = 0; i < m; i++)
        {
            for (size_t j = 0; j < m; j++)
            {
                cin >> maze[i][j];
                if (maze[i][j] == 'S')
                {
                    xs = i, ys = j;
                }
            }
        }
        bfs(xs, ys, m);
        cout << flag << endl;
    }
}